// 随机数字
 const   createRandomNumberGenerator = (machineId:number) => {
  let sequence = 0;           // 序列号
  let lastTimestamp = -1n;    // 上一个时间戳

  // 常量定义
  const workerIdBits = 5;     // 机器ID所占的位数
  const sequenceBits = 12;     // 序列号所占的位数
  const timestampLeftShift = BigInt(sequenceBits + workerIdBits); // 时间戳左移位数
  const maxWorkerId = (1n << BigInt(workerIdBits)) - 1n; // 最大机器ID
  const maxSequence = (1n << BigInt(sequenceBits)) - 1n; // 最大序列号

  const epoch = BigInt(1640995200000); // 自定义纪元（例如：2022-01-01T00:00:00.000Z）

  const currentTime = () => {
      return BigInt(new Date().getTime());
  };

  const waitNextMillis = (lastTimestamp:BigInt) => {
      let timestamp = currentTime();
      while (timestamp <= Number(lastTimestamp)) {
          timestamp = currentTime();
      }
      return timestamp;
  };

  const nextId = () => {
      var timestamp = currentTime();

      // 如果当前时间小于上一次生成ID的时间，则抛出异常
      if (timestamp < lastTimestamp) {
          throw new Error("Clock is moving backwards. Rejecting requests until " + lastTimestamp);
      }

      // 如果是同一时间生成，则增加序列号
      if (lastTimestamp === timestamp) {
          sequence = (sequence + 1) & Number(maxSequence); // 确保序列号不溢出
          if (sequence === 0) {
              // 如果序列号溢出，等待下一个时间戳
              timestamp = waitNextMillis(lastTimestamp);
          }
      } else {
          // 如果是不同时间生成，则重置序列号
          sequence = 0;
      }

      lastTimestamp = timestamp;

      // 组合各部分，生成ID
      return ((timestamp - epoch) * (1n << timestampLeftShift) + (BigInt(machineId) << BigInt(sequenceBits)) + BigInt(sequence)).toString();
  };

  return {
      nextId
  };
};


// 使用示例



// // 递归菜单
// const organizeMenuData = (arr:Array<object>, parentId = '') => {
//   const tree:any[]=[];
//   arr.forEach(item => {
//     if (item.parent_id == parentId) {
//       const children = organizeMenuData(arr, item.id);
//       if (children.length) {
//         item.children = children;
//       }
//       tree.push(item);
//     }
//   });
//   return tree;
// }


// 时间格式化函数
const formatDate1 = (date:any) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份从0开始
  const day = date.getDate();
  return `${year}-${month}-${day}`;
};





// 时间处理
const formatDate = (timeStamp:any, type = 'Y-M-D H:I:S', auto = false) => {
  let time = (timeStamp + '').length === 10 ? new Date(parseInt(timeStamp) * 1000) : new Date(parseInt(timeStamp));
  let _year = time.getFullYear();
  let _month = (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1);
  let _date = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
  let _hours = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
  let _minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
  let _secconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
  let formatTime = '';
  let distinctTime = new Date().getTime() - time.getTime();

  if (auto) {
    if (distinctTime <= (1 * 60 * 1000)) {
      // console.log('一分钟以内，以秒数计算');
      let _s = Math.floor((distinctTime / 1000) % 60);
      formatTime = _s + '秒前';
    } else if (distinctTime <= (1 * 3600 * 1000)) {
      // console.log('一小时以内,以分钟计算');
      let _m = Math.floor((distinctTime / (60 * 1000)) % 60);
      formatTime = _m + '分钟前';
    } else if (distinctTime <= (24 * 3600 * 1000)) {
      // console.log('一天以内，以小时计算');
      let _h = Math.floor((distinctTime / (60 * 60 * 1000)) % 24);
      formatTime = _h + '小时前';
    } else if (distinctTime <= (30 * 24 * 3600 * 1000)) {
      let _d = Math.floor((distinctTime / (24 * 60 * 60 * 1000)) % 30);
      formatTime = _d + '天前';
      // console.log('30天以内,以天数计算');
    } else {
      // 30天以外只显示年月日
      formatTime = _year + '-' + _month + '-' + _date;
    }
  } else {

  switch (type) {
    case 'Y-M-D H:I:S':
      formatTime = _year + '-' + _month + '-' + _date + ' ' + _hours + ':' + _minutes + ':' + _secconds;
      break;
    case 'Y-M-D H:I:S zh':
      formatTime = _year + '年' + _month + '月' + _date + '日  ' + _hours + ':' + _minutes + ':' + _secconds;
      break;
    case 'Y-M-D H:I':
      formatTime = _year + '-' + _month + '-' + _date + ' ' + _hours + ':' + _minutes;
      break;
    case 'Y-M-D H':
      formatTime = _year + '-' + _month + '-' + _date + ' ' + _hours;
      break;
    case 'Y-M-D':
      formatTime = _year + '-' + _month + '-' + _date;
      break;
    case 'Y-M-D zh':
      formatTime = _year + '年' + _month + '月' + _date + '日';
      break;
    case 'Y-M':
      formatTime = _year + '-' + _month;
      break;
    case 'Y':
      formatTime = String(_year);
      break;
    case 'M':
      formatTime = String(_month);
      break;
    case 'D':
      formatTime = String(_date);
      break;
    case 'H':
      formatTime =String( _hours);
      break;
    case 'I':
      formatTime =String( _minutes);
      break;
    case 'S':
      formatTime = String(_secconds);
      break;
    default:
      formatTime = _year + '-' + _month + '-' + _date + ' ' + _hours + ':' + _minutes + ':' + _secconds;
      break;
  }
  }
  // 返回格式化的日期字符串
  return formatTime;
}

// // 取掉字符串两边指定字符
// const deleteStrByWay = (str, char) => {
//   if (char) {
//     str = str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
//   }
//   return str.replace(/^\s+|\s+$/g, '');
// }

export default {createRandomNumberGenerator,formatDate1,formatDate}
